package com.tao.com.xiaowen.module.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.tao.com.xiaowen.adapter.JokePagerAdapter;
import com.tao.com.xiaowen.R;

/**
 * Created by Administrator on 2016/8/1.
 */
public class JokeFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    public static final int TEXT_JOKE = 0;
    public static final int PIC_JOKE = 1;

    private ViewPager viewPager;

    private RadioGroup radioGroup;
    private RadioButton btnText;
    private RadioButton btnPicture;
    private JokePagerAdapter jokeAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_joke, container, false);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
        btnText = (RadioButton) view.findViewById(R.id.text_joke);
        btnPicture = (RadioButton) view.findViewById(R.id.pic_joke);

        radioGroup.setOnCheckedChangeListener(this);
        viewPager.addOnPageChangeListener(this);

        jokeAdapter = new JokePagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(jokeAdapter);
        viewPager.setCurrentItem(TEXT_JOKE);
        btnText.setTextSize(20);
        return view;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
        switch (checkId) {
            case R.id.text_joke:
                btnText.setTextSize(20);
                btnPicture.setTextSize(16);
                viewPager.setCurrentItem(TEXT_JOKE);
                break;
            case R.id.pic_joke:
                btnText.setTextSize(16);
                btnPicture.setTextSize(20);
                viewPager.setCurrentItem(PIC_JOKE);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case TEXT_JOKE:
                btnText.setTextSize(20);
                btnPicture.setTextSize(16);
                radioGroup.check(R.id.text_joke);
                break;
            case PIC_JOKE:
                btnText.setTextSize(16);
                btnPicture.setTextSize(20);
                radioGroup.check(R.id.pic_joke);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
